package com.wp.service.impl;

import com.wp.mapper.BlogSortMapper;
import com.wp.pojo.BlogSort;
import com.wp.pojo.PageResult;
import com.wp.pojo.basePojo.SysResult;
import com.wp.service.BlogSortService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class BlogSortServiceImpl implements BlogSortService {
    @Autowired
    private BlogSortMapper blogSortMapper;
    /**
     * 新增博客分类
     *
     * @return*/
    @Override
    public SysResult addBlogSort(BlogSort blogSort) {
        Date date = new Date();
        blogSort.setCreateTime(date);
        blogSort.setUpdateTime(date);
//        String uid = UUID.randomUUID().toString().replace("-","");
//        blogSort.setUid(uid);
//        blogSort.setStatus(1);
//        blogSortMapper.addBlogSort(blogSort);

        String blogSort1 = blogSortMapper.getBlogSort(blogSort.getSortName());
        if (blogSort1 != null){
            return SysResult.fail();
        }else {
            String uid = UUID.randomUUID().toString().replace("-","");
//            BlogSort blogSort2 = new BlogSort();
//            blogSort2.setSortName(blogSort.getSortName());
//            blogSort2.setSort(blogSort.getSort());
//            blogSort2.setContent(blogSort.getContent());
//            blogSort2.setUid(uid).setStatus(1);
            String name = blogSort.getSortName();
            Integer sort = blogSort.getSort();
            String content = blogSort.getContent();
            Integer status = blogSort.getStatus();
             blogSortMapper.addSort(uid,name,sort,content,status);
            return SysResult.success();
        }
    }
    /**
     * 编辑博客分类
     * */
    @Override
    public void ediBlogsort(BlogSort blogSort){
        blogSort.setUpdateTime(new Date());
        blogSortMapper.updateedit(blogSort);
    }
//    @Override
//    public void editsort(BlogSort blogSort) {
//        blogSort.setUpdateTime(new Date());
//        blogSort.setStatus(1);
//        blogSort.setClickCount(0);
//        blogSort.setSort(0);
////        String uid = blogSort.getUid();
//        blogSortMapper.editSort(blogSort);
//
//    }
    /**
     * 删除博客分类
     * */
//    @Override
//    public void deleteBlogsort(String uid){
//        Date updateTime = new Date();
//        blogSortMapper.deleteSort(uid,updateTime);
//    }
    @Override
    public void deletesort(BlogSort blogSort) {
        blogSort.setUpdateTime(new Date());
        blogSort.setStatus(0); //status 从1到0，封停状态表示删除
        blogSortMapper.deleteSort(blogSort);
    }
    /**
     * 获取博客分类列表
     * */
    @Override
    public PageResult getsortList(PageResult pageResult) {
        Integer status = 1;
        String query = pageResult.getQuery();
        Integer num = pageResult.getPageNum();
        Integer size= pageResult.getPageSize();
        Integer startNum= (num-1)*size;
        Long total = blogSortMapper.getSortCount(query,status);
        List<BlogSort> pageResults= blogSortMapper.getSortList(query,status,startNum,size);
        pageResult.setTotal(total);
        pageResult.setRows(pageResults);
        return pageResult;
    }
}
